package com.lh.process.engine.instance;

import com.lh.process.engine.context.ProcessContextFactory;
import com.lh.process.engine.process.Processor;
import com.lh.process.engine.util.SpringUtil;
import org.springframework.stereotype.Component;

/**
 * @author: linghao
 * @since: 2022/10/25
 */
@Component
public class SpringBeanInstanceCreator implements ProcessorInstanceCreator{

    @Override
    public Processor newInstance(String className, String name) throws Exception {
        Object bean;
        try {
            Class<?> aClass = Class.forName(className);
            bean = SpringUtil.getBean(aClass);
        }catch (Exception e){
            return ProcessContextFactory.defaultProcessInstanceCreator.newInstance(className,name);
        }
        if(!(bean instanceof Processor)){
            throw new IllegalArgumentException("类" + className + "不是Processor实例");
        }
        Processor processor= (Processor)bean;
        processor.setName(name);
        return processor;
    }
}
